package ljl.alg.jianzhioffer.round3.medium;

public class _20_isNumber {

    /*
    * epart ::= [eE] signedInt
    * signedInt ::= [+-]? unsignedInt
    * unsignedInt ::= [0-9]+
    * float ::= signedInt '.' unsignedInt epart? | '.' unsignedInt epart? |
    *           signedInt '.' epart?
    * */

    int i;
    String s;
    public boolean isNumber(String s) {
        this.s = s.trim();
        boolean numeric = signed();
        if (next() == '.') {
            i++;
            numeric |= unsigned();
        }

        char c = next();
        if (c == 'e' || c == 'E') {
            i++;
            numeric &= signed();
        }
        return numeric && i == s.length();
    }

    char next() {
        return i == s.length() ? 0 : s.charAt(i);
    }

    boolean signed() {
        char c = next();
        if (c == '+' || c == '-') i++;
        return unsigned();
    }

    boolean unsigned() {
        int oldI = i;
        while (Character.isDigit(next())) i++;
        return i - oldI > 0;
    }

}
